package hn.cch.configuration;

import hn.cch.constant.UrlConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationEntryPoint authenticationEntryPointImpl;

    @Autowired
    private AuthenticationSuccessHandler successHandlerImpl;

    @Autowired
    private AuthenticationFailureHandler failureHandlerImpl;

    @Autowired
    private LogoutSuccessHandler logoutSuccessHandlerImpl;

    @Autowired
    private AccessDeniedHandler accessDeniedHandlerImpl;

    // @Autowired
    // private SecurityRequestFilter securityRequestFilter;

    // @Autowired
    // private UserDetailsService userDetailsService;
    //
    // @Autowired
    // private AuthenticationProvider authenticationProvider;

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
        // auth.authenticationProvider(authenticationProvider);

        auth.inMemoryAuthentication().withUser("admin").password("chench").roles("ADMIN")
                .and().withUser("root").password("chench").roles("USER")
        ;
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/images/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        // http.addFilterBefore(securityRequestFilter, AbstractPreAuthenticatedProcessingFilter.class);
        // http.addFilterBefore(securityRequestFilter, AbstractPreAuthenticatedProcessingFilter.class);
        // http.addFilterBefore(securityRequestFilter, UsernamePasswordAuthenticationFilter.class);
        http.httpBasic()
                .and()
                .authorizeRequests()
                .antMatchers(UrlConstant.PASS_URL_LIST).permitAll()
                .anyRequest().authenticated()

                // 会话
                // .and()
                // .sessionManagement()
                // .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                // 异常
                .and()
                .exceptionHandling()
                // .authenticationEntryPoint(authenticationEntryPointImpl)
                // .accessDeniedHandler(accessDeniedHandlerImpl)

                // 登录
                .and()
                .formLogin()
                // .loginPage(UrlConstant.LOGIN_PAGE_URL)
                .loginProcessingUrl(UrlConstant.POST_LOGIN_URL)
                .usernameParameter("username")
                .passwordParameter("password")
                .successHandler(successHandlerImpl)
                .failureHandler(failureHandlerImpl)
                // todo 记住我
                // .and().rememberMe().tokenValiditySeconds(60 * 60).tokenRepository()
                // 注销
                .and()
                .logout()
                .logoutUrl(UrlConstant.GET_LOGOUT_URL)
                .logoutSuccessHandler(logoutSuccessHandlerImpl)
                // .deleteCookies("JSESSIONID")
                // .clearAuthentication(true)
                // .invalidateHttpSession(true)
                // .and()
                // .cors()
                // // .disable()
                .and()
                .csrf()
                .disable()

        ;

    }
}
